package cn.edu.nwsuaf.teachercenter.aop;

import java.lang.reflect.Method;


import org.apache.log4j.Logger;
import org.springframework.aop.AfterReturningAdvice;
import org.springframework.aop.MethodBeforeAdvice;
import org.springframework.aop.ThrowsAdvice;

import javassist.ClassClassPath;
import javassist.ClassPool;
import javassist.CtClass;
import javassist.CtMethod;
import javassist.bytecode.AnnotationsAttribute;
import javassist.bytecode.ConstPool;
import javassist.bytecode.MethodInfo;
import javassist.bytecode.annotation.Annotation;
import javassist.bytecode.annotation.EnumMemberValue;

/****
 * 用于解决动态切换hibernate加载策略，后发现无作用
 * @author Fantacy
 *
 */
public class JsonFetchTypeAdvice implements MethodBeforeAdvice, AfterReturningAdvice, ThrowsAdvice{

	private Logger log = Logger.getLogger(DataSourceAdvice.class);
	
	@Override
	public void afterReturning(Object arg0, Method arg1, Object[] arg2, Object arg3) throws Throwable {
		// TODO Auto-generated method stub
		ClassPool pool = ClassPool.getDefault();  
	    //获取要修改的类的所有信息  
		//pool.insertClassPath(this.getClass().getClassLoader().loadClass(Sc.class.getName()).getName());
	    pool.insertClassPath(new ClassClassPath(this.getClass()));
		CtClass ct = pool.get("cn.edu.nwsuaf.examcenter.pojo.Sc");
	    CtMethod[] cms = ct.getDeclaredMethods();
	    for(CtMethod cm:cms){
	    	Object[] obj = cm.getAnnotations();
	    	for(Object o:obj){
	    		//System.out.println(o.toString());
	    		if(o.toString().contains("ManyToOne")){
	    			//System.out.println(o.toString());
	    			MethodInfo methodInfo = cm.getMethodInfo();           
	    		    
	    	         AnnotationsAttribute attribute1 = (AnnotationsAttribute) methodInfo.getAttribute(AnnotationsAttribute.visibleTag);
	    		     System.out.println(methodInfo.getName()+"---"+attribute1);
	    		}
	    	}
	    	
	    }
	}

	@Override
	public void before(Method method, Object[] args, Object target) throws Throwable {
		
		ClassPool pool = ClassPool.getDefault();  
	    //获取要修改的类的所有信息  
		//pool.insertClassPath(this.getClass().getClassLoader().loadClass(Sc.class.getName()).getName());
	    pool.insertClassPath(new ClassClassPath(this.getClass()));
		CtClass ct = pool.get("cn.edu.nwsuaf.examcenter.pojo.Sc");
	    CtMethod[] cms = ct.getDeclaredMethods();
	    for(CtMethod cm:cms){
	    	Object[] obj = cm.getAnnotations();
	    	for(Object o:obj){
	    		//System.out.println(o.toString());
	    		if(o.toString().contains("ManyToOne")){
	    			//System.out.println(o.toString());
	    			MethodInfo methodInfo = cm.getMethodInfo();           
	    		     //获取类里的em属性  
	    		     AnnotationsAttribute attribute = (AnnotationsAttribute) methodInfo.getAttribute(AnnotationsAttribute.visibleTag);
	    		     System.out.println(attribute);
	    		     Annotation[] annotations = attribute.getAnnotations();
	    		     ConstPool cp = methodInfo.getConstPool();
	    		     AnnotationsAttribute attribute2 = new AnnotationsAttribute(cp, AnnotationsAttribute.visibleTag);  
	    	         Annotation annotation = new Annotation("javax.persistence.ManyToOne", cp);
	    	         EnumMemberValue enumMemberValue = new EnumMemberValue(cp);
	    	         enumMemberValue.setType("javax.persistence.FetchType");
	    	         enumMemberValue.setValue("EAGER");
	    	         annotation.addMemberValue("fetch", enumMemberValue);
	    	         
	    	         annotations[0] = annotation; 
	    	         attribute2.setAnnotations(annotations);
	    	         methodInfo.addAttribute(attribute2); 
	    	         
	    	         AnnotationsAttribute attribute1 = (AnnotationsAttribute) methodInfo.getAttribute(AnnotationsAttribute.visibleTag);
	    		     System.out.println(methodInfo.getName()+"---"+attribute1);
	    		}
	    	}
	    	
	    }
		log.info("切入的方法名为:"+method.getName());
	}
	public void afterThrowing(Method method, Object[] args, Object target, Exception ex) throws Throwable {
		//DataSourceSwitcher.setSlave();
		log.info("出现异常,切换到: slave");
	}

}
